var flag = 0;
var pageNo = 1;
var pageSize = 10;
var check = -1;
var businessCategoryId = "";
var airportTerminalGateId="";
var airports = "";
var pageNum="";
showList();
/////店铺类型
$("document").ready(function () {
    ajax({
        url: "business/queryBusinessCategory",
        data: {},
        success: function (data) {
            var html = template("styleTemplate", data);
            document.getElementById('shopType').innerHTML = html;
        }
    });
});

//加载登机
function boardingGate(){
    ajax({
        url:"airportInfoTerminalGate/queryAirportInfoGateListByAirportId",
        data:{airportId:airports},
        success:function(data){
            
            var html=template("boardingGateTemplate",data);
            document.getElementById('boardingGate').innerHTML = html;
        }
    });
}

// //搜索按钮
//
// $("#searchBtn").click(function () {
//     pageNo = 1;
//     showList()
// });

//关联店铺
$("#submit").click(function () {
    chooseShops();
});

function chooseShops() {
    var bussinessConnect = [];
    var bussinessConnectObj = {}
    $(".i-checks").each(function (i, d) {
        if ($(d).children().hasClass("checked")) {
            bussinessConnectObj = {
                biid: $(d).attr("biid"),
                biName: $(d).attr("biName")
            };
            bussinessConnect.push(bussinessConnectObj)
        }
    });
    if (bussinessConnect.length == 0) {
        // parent.layer.msg("请勾选");
        delete window.localStorage.shopsConnect;
    }else{
        setStorJson("shopsConnect", bussinessConnect);
        
    }
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
}
/////table的ajax请求
function showList() {
    if (flag !== 0) {
        return;
    }
    flag = 1;
    if (getStorJson("airportConnect") != null) {
        airports = getStorJson("airportConnect").aiId;
    }
    ajax({
        url: "business/queryBusinessData",
        data: {
            //多个机场id
            airports: airports,
            //店铺名称
            biName: "",
            //店铺编号
            bNum: "",
            //店铺人员
            userName: "",
            //服务电话
            contactNum: "",
            //登机口
            airportTerminalGateId: airportTerminalGateId,
            //店铺类型
            businessCategoryId: businessCategoryId,
            //第几页
            pageNo: pageNo,
            //页面大小
            pageSize: pageSize,
            //请求状态
            requestStatus: check
        },
        success: function (data) {
            
            flag = 0;
            temp(data);
            createPageInfo(data.data.queryBusinessData);
            $("#checkAll").html("全选");
        }
    });
}


//请求成功后执行的table列表
function temp(data) {
    var count = data.data.queryBusinessAmount;
    $('#total').html(count.total);
    $('#signed').html(count.signed);
    $('#noSign').html(count.noSign);
    
    var shopData = data.data.queryBusinessData;
    var html = template("template", shopData);
    document.getElementById('tbody').innerHTML = html;
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green'
    });
    if (getStorJson("shopsConnect") != null) {        
        $.each(getStorJson("shopsConnect"), function (j, f) {
            $(".i-checks").each(function (i, d) {            
                if ($(d).attr("biid") == f.biid) {
                    $(this).iCheck('check')
                }
            })
        })
    }

}

//选择店铺类型
$('#shopType').change(function () {
    businessCategoryId = $(this).val();
    pageNo=1;
    showList();
});
//选择登机口
$('#boardingGate').change(function () {
    airportTerminalGateId=$(this).val();
    pageNo=1;
    showList();
});

//按键颜色
$(".btnStyle").click(function(){
    $(".btnStyle").removeClass("btnColor");
    $(this).addClass("btnColor");
});

//改变页面显示数量
$("#pageSizeInfo").change(function () {
    $("#pageSizeInfo").blur()
    if (flag == 1) {
        return
    }
    pageSize = this.options[this.options.selectedIndex].value;
    pageNo=1;
    showList();
});

//全选/全不选
$("#checkAll").click(function () {
    if ($(this).text() == "全选") {
        $('.i-checks').iCheck('check');
        $(this).text("不选")
    } else {
        $('.i-checks').iCheck('uncheck');
        $(this).text("全选")
    }
});

////审核状态
$('.verify>.btn-outline').on("click", function () {
    if ($(this).hasClass("total")) {
        check = -1;
    } else if ($(this).hasClass("signed")) {
        check = 1;
    } else if ($(this).hasClass("noSign")) {
        check = 0;
    }
    pageNo = 1;
    showList();
});


//创建页码信息
function createPageInfo(data) {
    $("#startRow").text(data.startRow);
    $("#endRow").text(data.endRow);
    $("#totalPage").text(data.total);
    pageNum=data.pages
    var temp = '';
    for (var i = 1; i < data.pages + 1; i++) {
        temp += '<option value="' + i + '">' + i + '</option>'
    }
    $("#pageInfo").html(temp)
        .val(data.pageNum)
        .change(function () {
            $("#pageInfor").blur(); 
            if (flag == 1) {
                return
            }
            pageNo = this.options[this.options.selectedIndex].value;
            showList();
        });
    $("#firstPage").click(function () {
        if (pageNo != 1) {
            if (flag == 1) {
                return
            }
            pageNo = 1;
            showList();
        }
    });
    $("#lastPage").click(function () {
        if (pageNo < pageNum) {
            if (flag == 1) {
                return
            }
            pageNo = pageNum;
            showList();
        }
    });
    $("#nextPage").click(function () {
        if (pageNo < pageNum) {
          
            if (flag == 1) {
                
                return
            }
            pageNo++;
            showList();
        }
    });
    $("#previousPage").click(function () {
        if (pageNo > 1) {
            if (flag == 1) {
                return
            }
            pageNo--;
            showList();
        }
    });
}